package com.ruoyi.note.service.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.note.domain.NoteDaily;
import com.ruoyi.note.mapper.NoteDailyMapper;
import com.ruoyi.note.mapper.NoteAttachmentMapper;
import com.ruoyi.note.service.INoteDailyService;

/**
 * 日常笔记Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class NoteDailyServiceImpl implements INoteDailyService 
{
    private static final Logger logger = LoggerFactory.getLogger(NoteDailyServiceImpl.class);
    
    @Autowired
    private NoteDailyMapper noteDailyMapper;

    @Autowired
    private NoteAttachmentMapper noteAttachmentMapper;

    /**
     * 查询日常笔记
     * 
     * @param noteId 日常笔记主键
     * @return 日常笔记
     */
    @Override
    public NoteDaily selectNoteDailyByNoteId(Long noteId)
    {
        NoteDaily noteDaily = noteDailyMapper.selectNoteDailyByNoteId(noteId);
        if (noteDaily != null)
        {
            // 查询附件列表
            noteDaily.setAttachments(noteAttachmentMapper.selectAttachmentsByRelation("1", noteId));
        }
        return noteDaily;
    }

    /**
     * 查询日常笔记列表
     * 
     * @param noteDaily 日常笔记
     * @return 日常笔记
     */
    @Override
    public List<NoteDaily> selectNoteDailyList(NoteDaily noteDaily)
    {
        return noteDailyMapper.selectNoteDailyList(noteDaily);
    }

    /**
     * 新增日常笔记
     * 
     * @param noteDaily 日常笔记
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNoteDaily(NoteDaily noteDaily)
    {
        int rows = noteDailyMapper.insertNoteDaily(noteDaily);
        insertNoteAttachment(noteDaily);
        return rows;
    }

    /**
     * 修改日常笔记
     * 
     * @param noteDaily 日常笔记
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNoteDaily(NoteDaily noteDaily)
    {
        try {
            logger.info("开始更新日常笔记，笔记ID: {}", noteDaily.getNoteId());
            // 删除原有附件
            noteAttachmentMapper.deleteAttachmentsByRelation("1", noteDaily.getNoteId());
            // 新增附件
            insertNoteAttachment(noteDaily);
            int result = noteDailyMapper.updateNoteDaily(noteDaily);
            logger.info("更新日常笔记完成，笔记ID: {}, 更新结果: {}", noteDaily.getNoteId(), result);
            return result;
        } catch (Exception e) {
            logger.error("修改日常笔记失败，笔记ID: " + noteDaily.getNoteId(), e);
            throw new ServiceException("修改日常笔记失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除日常笔记
     * 
     * @param noteIds 需要删除的日常笔记主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoteDailyByNoteIds(String noteIds)
    {
        Long[] noteIdArray = Convert.toLongArray(noteIds);
        // 删除附件
        for (Long noteId : noteIdArray)
        {
            noteAttachmentMapper.deleteAttachmentsByRelation("1", noteId);
        }
        return noteDailyMapper.deleteNoteDailyByNoteIds(noteIdArray);
    }

    /**
     * 删除日常笔记信息
     * 
     * @param noteId 日常笔记主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoteDailyByNoteId(Long noteId)
    {
        // 删除附件
        noteAttachmentMapper.deleteAttachmentsByRelation("1", noteId);
        return noteDailyMapper.deleteNoteDailyByNoteId(noteId);
    }

    /**
     * 增加阅读次数
     * 
     * @param noteId 笔记ID
     * @return 结果
     */
    @Override
    public int increaseViewCount(Long noteId)
    {
        return noteDailyMapper.increaseViewCount(noteId);
    }

    /**
     * 新增笔记附件信息
     * 
     * @param noteDaily 笔记对象
     */
    public void insertNoteAttachment(NoteDaily noteDaily)
    {
        try {
            if (noteDaily.getAttachments() != null && noteDaily.getAttachments().size() > 0)
            {
                noteDaily.getAttachments().forEach(attachment -> {
                    attachment.setRelationType("1");
                    attachment.setRelationId(noteDaily.getNoteId());
                    noteAttachmentMapper.insertNoteAttachment(attachment);
                });
            }
        } catch (Exception e) {
            logger.error("新增笔记附件信息失败，笔记ID: " + noteDaily.getNoteId(), e);
            throw new ServiceException("新增笔记附件信息失败: " + e.getMessage());
        }
    }
}